package com.modules.controller.sys;


import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.common.controller.MyController;
import com.common.log.LogData;
import com.common.vo.Result;
import com.modules.entity.sys.DictEntity;
import com.modules.entity.sys.DictSettingEntity;
import com.modules.service.sys.DictService;

import cn.hutool.core.util.StrUtil;


/**
 * 字典
 * @author yyq
 *
 */
@Controller
@RequestMapping("/sys/dict")
public class DictController extends MyController<DictEntity>{

	@Autowired
	private DictService service;
	
	/**
	 * 查询
	 * @param dict
	 * @return
	 */
	@RequestMapping("/select")
	@ResponseBody
	public Result<?> select(DictEntity dict){
		LambdaQueryWrapper<DictEntity> queryWrapper = new LambdaQueryWrapper<DictEntity>();
		if(StrUtil.isNotBlank(dict.getDictName())) {
			queryWrapper.like(DictEntity::getDictName, dict.getDictName());
		}else if(StrUtil.isNotBlank(dict.getDictCode())) {
			queryWrapper.like(DictEntity::getDictCode, dict.getDictCode());
		}else if(dict.getDictType() != null) {
			queryWrapper.like(DictEntity::getDictType, dict.getDictType());
		} 
		IPage<DictEntity> iPage = service.page(getPage(), queryWrapper);
		return Result.success(iPage.getRecords(), iPage.getTotal());
	}


	/**
	 * 保存
	 * @param dict
	 * @return
	 */
	@LogData(module = "字典管理", remarks = "保存")
	@RequestMapping("/save")
	@ResponseBody
	public Result<?> save(DictEntity dict){
		LambdaQueryWrapper<DictEntity> wrapper = new LambdaQueryWrapper<DictEntity>();
		if(dict.getId() != null) {
			wrapper.ne(DictEntity::getId, dict.getId());
		}
		wrapper.eq(DictEntity::getDictCode, dict.getDictCode());
		DictEntity one = service.getOne(wrapper);
		if(one != null) {
			return Result.fail("操作失败，重复编码");
		}
		service.saveOrUpdate(dict);
		return Result.success();
	}
	
	/**
	 * 删除
	 * @return
	 */
	@LogData(module = "字典管理", remarks = "删除单个")
	@RequestMapping("/delete")
	@ResponseBody
	public Result<?> delete(String id){
		service.removeById(id);
		return Result.success();
	}
	
	/**
	 * 批量删除
	 * @return
	 */
	@LogData(module = "字典管理", remarks = "批量删除")
	@RequestMapping("/deleteBatch")
	@ResponseBody
	public Result<?> deleteBatch(String ids){
		service.removeByIds(Arrays.asList(ids.split(",")));
		return Result.success();
	}
	
	/**
	 * 查询参数设置
	 * @param dict
	 * @return
	 */
	@RequestMapping("/selectSetting")
	@ResponseBody
	public Result<?> selectSetting(@RequestParam(value="did",defaultValue="0",required=true) Integer did,
			@RequestParam(value="page",defaultValue="1",required=true) Integer page,
			@RequestParam(value="limit",defaultValue="10",required=true) Integer limit){
		return service.selectSetting(new Page<>(page, limit), did);
	}
	
	/**
	 * 保存参数设置
	 * @param dict
	 * @return
	 */
	@LogData(module = "字典管理-参数设置", remarks = "保存")
	@RequestMapping("/saveSetting")
	@ResponseBody	public Result<?> saveSetting(DictSettingEntity dict){
		return service.saveOrUpdateSetting(dict);
	}
	
	/**
	 * 删除参数设置
	 * @return
	 */
	@LogData(module = "字典管理-参数设置", remarks = "删除单个")
	@RequestMapping("/deleteSetting")
	@ResponseBody
	public Result<?> deleteSetting(String id){
		return service.removeByIdSetting(id);
	}
	
	/**
	 * 修改状态
	 * @return
	 */
	@LogData(module = "字典管理-参数设置", remarks = "修改状态")
	@RequestMapping("/updateSettingState")
	@ResponseBody
	public Result<?> updateSettingState(DictSettingEntity dict){
		return service.updateSettingState(dict);
	}
	
	/**
	 * 获取所以数字字典
	 * @return
	 */
	@RequestMapping("/getDictAll")
	@ResponseBody
	public Result<?> getDictAll(String ids){
		return service.getDictAll();
	}

	/**
	 * 获取字典text
	 * @return
	 */
	@RequestMapping("/getDictText")
	@ResponseBody
	public Result<?> getDictText(String code, String value){
		String text = service.getDictText(code, value);
		return Result.success(text);
	}
	
	/**
	 * 获取字典所有参数
	 * @return
	 */
	@RequestMapping("/getDictSetting")
	@ResponseBody
	public Result<?> getDictSetting(String code){
		List<DictSettingEntity> list = service.getDictSetting(code);
		return Result.success(list);
	}
	
	

}
